package com.sen.controller;

import com.sen.config.security.MyUserDetail;
import com.sen.result.Result;
import com.sen.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@Api(tags = "登录控制器")
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Result login(@RequestParam String username,@RequestParam  String password){
        return loginService.login(username, password);
    }

    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public Result logout(){
        // 退出登录只需要在前端把token信息给清除即可
        return Result.ok().msg("已退出登录");
    }

    @ApiOperation(value = "查询当前登录用户的详细信息")
    @GetMapping("/getLoinUserInfo")
    public Result UserInfo(){
        MyUserDetail userDetail = (MyUserDetail)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return Result.ok().data("data", userDetail);
    }

}
